make_lbg_data <- function(){
  dd <- matrix(0, nrow=37, ncol=6)
  dimnames(dd) <- list(words=c(LETTERS, paste('Z', LETTERS, sep='')[1:11]), 
                       docs=c(paste('R', 1:5, sep=''), 'V1')) 
  rw <- c(2,3,10,22,45,78,115,146,158,146,115,78,45,22,10,3,2)
  ins <- function(start=0){ s <- rep(0,37); s[start:(start+length(rw)-1)] <- rw; s }
  dd[,1] <- ins(1)
  dd[,2] <- ins(6)
  dd[,3] <- ins(11)
  dd[,4] <- ins(16)
  dd[,5] <- ins(21)
  dd[,6] <- ins(8)
  dd
}

figure1 <- function(){
  dd <- make_lbg_data()  

  plot(dd[,'R5'], type='l', axes=FALSE, ylab="Word Count", xlab="Word")
  lines(dd[,'R4'])
  lines(dd[,'R3'])
  lines(dd[,'R2'])
  lines(dd[,'R1'])
  lines(dd[,'V1'], lwd=3)
  axis(1, at=1:37, labels=rownames(dd))
  axis(2, at=c(0,50,100,150))
}

figure2 <- function(){
  dd <- make_lbg_data()  

  filtered <- rownames(dd)
  filtered[seq(2,37,2)] <- ""

  ws <- seq(-2.7,2.7,0.15)
  plot(ws, 1:37, type='n', xlab="Correct Score", ylab="Word", axes=FALSE)
  axis(1, at=seq(-2.5, 2.5, by=.5), labels=seq(-2.5, 2.5, by=.5))
  axis(2, at=1:37, labels=filtered, las=1)
  xseg0 <- rep(-2.7,37)
  xseg1 <- rep(2.7,37)
  yseg0 <- 1:37
  yseg1 <- 1:37
  segments(xseg0, yseg0, xseg1, yseg1, lty=3, lwd=1, col='gray')
  points(ws, 1:37, pch=19)
}

## This is for demonstration  
## Use the functions in the R package 'austin' for real work
compute_wordscores <- function(dd, scores){
    ## dd is words by documents, all scored
    F <- scale(dd, center = FALSE, scale = colSums(dd))
    ws <- (F %*% scores) / rowSums(F)
    ws
}

figure3 <- function(){
  dd <- make_lbg_data()	

  filtered <- row.names(dd)
  filtered[seq(2,37,2)] <- ""
  ws <- compute_wordscores(dd[,1:5], seq(-1.5,1.5,by=.75)) 

  plot(ws[,1], 1:37, type='n',
       xlab="Estimated Score", ylab="Word",
       axes=FALSE, xlim=c(-2.7,2.7))
  axis(1, at=seq(-2.5, 2.5, by=.5),
       labels=seq(-2.5, 2.5, by=.5))
  axis(2, at=1:37,
       labels=filtered, las=1)
  xseg0 <- rep(-2.7,37)
  xseg1 <- rep(2.7,37)
  yseg0 <- 1:37
  yseg1 <- 1:37
  segments(xseg0, yseg0, xseg1, yseg1, lty="dotted", lwd=1, col='gray')
  points(seq(-2.7,2.7,.15), 1:37, pch=19, col='gray')
  points(ws[,1], 1:37, pch=19)
}

figure4 <- function(){
  dd <- make_lbg_data()	

  filtered <- row.names(dd)
  filtered[seq(2,37,2)] <- ""
  ws <- compute_wordscores(dd[,c(1,3,5)], c(-1.5,0,1.5)) 

  plot(ws[,1], 1:37, type='n',
       xlab="Estimated Score", ylab="Word",
       axes=FALSE, xlim=c(-2.7,2.7))
  axis(1, at=seq(-2.5, 2.5, by=.5),
       labels=seq(-2.5, 2.5, by=.5))
  axis(2, at=1:37,
       labels=filtered, las=1)
  xseg0 <- rep(-2.7,37)
  xseg1 <- rep(2.7,37)
  yseg0 <- 1:37
  yseg1 <- 1:37
  segments(xseg0, yseg0, xseg1, yseg1, lty="dotted", lwd=1, col='gray')
  points(seq(-2.7,2.7,.15), 1:37, pch=19, col='gray')
  points(ws[,1], 1:37, pch=19)
}

figure5 <- function(){
  data <- read.csv('manfreqs.csv', row.names=1)
  dd <- data[,c(1,3,5)]
  dd <- dd[rowSums(dd)>0,] ## remove words that do not appear in these manifestos
  econ.scores <- c(17.2,5.35,8.21)
  ws <- compute_wordscores(dd, econ.scores)
  plot(ws[order(ws)], 1:length(ws), xlim=c(0,20), xlab='Estimated Score', ylab='Word')
}
        

